
package com.heimali.happyreader.utils;

import android.annotation.SuppressLint;
import android.content.Context;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;

@SuppressLint("SimpleDateFormat")
public class Utils {
    public final static <T> T[] asArray(T... args) {
        return args;
    }

    public final static SimpleDateFormat DATE_FORMAT_1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public final static SimpleDateFormat DATE_FORMAT_2 = new SimpleDateFormat("yyyy年MM月dd日");
    public final static SimpleDateFormat DATE_FORMAT_3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public final static SimpleDateFormat DATE_FORMAT_4 = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SS");
    public final static SimpleDateFormat DATE_FORMAT_5 = new SimpleDateFormat("yyyy-MM-dd");
    public final static SimpleDateFormat DATE_FORMAT_6 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");


    public static byte[] MD5(byte[] input) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        if (md != null) {
            md.update(input);
            return md.digest();
        } else
            return null;
    }

    public static String getMD5(byte[] input) {
        return Utils.bytesToHexString(MD5(input));
    }

    public static String getMD5(String input) {
        return getMD5(input.getBytes());
    }
    
    /**
     * Converts a byte array into a String hexidecimal characters
     * 
     * null returns null
     */
    public static String bytesToHexString(byte[] bytes) {
        if (bytes == null)
            return null;
        String table = "0123456789abcdef";
        StringBuilder ret = new StringBuilder(2 * bytes.length);

        for (int i = 0; i < bytes.length; i++) {
            int b;
            b = 0x0f & (bytes[i] >> 4);
            ret.append(table.charAt(b));
            b = 0x0f & bytes[i];
            ret.append(table.charAt(b));
        }

        return ret.toString();
    }
    
    /** 将 dip 单位的尺寸换算乘 px */
    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return Math.round(dipValue * scale);
    };
}
